/**
 * 
 */
package com.xwtech.cring.admin.controller;

import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;

import com.xwtech.cms.data.AbstractDataAdapter;
import com.xwtech.cms.data.db.JdbcTransaction;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.xwtech.cring.admin.bo.BgUserBO;
import com.xwtech.cring.admin.pub.AdminUserRetConst;
import com.xwtech.cring.pub.po.SysUserInfo;
import com.xwtech.framework.pub.po.FrameLogin;
import com.xwtech.framework.pub.result.ResultConstants;
import com.xwtech.framework.pub.result.ResultInfo;
import com.xwtech.framework.pub.result.ResultInfos;
import com.xwtech.framework.pub.utils.StringUtils;
import com.xwtech.framework.pub.web.RequestNameConstants;

/**
 * 
 * <p>
 * Title: 用户信息修改与删除管理功能模块
 * </p>
 * 
 * <p>
 * Description: 用户信息修改与删除管理
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 * 
 * <p>
 * Company: xwtech.com
 * </p>
 * 
 * @author not attributable
 * @version 1.0
 */
public class BgUserEditDelController extends MultiActionController {

	protected static final Logger logger = Logger
			.getLogger(BgUserEditDelController.class);

	private BgUserBO bgUserBO;

	public BgUserBO getBgUserBO() {
		return bgUserBO;
	}

	public void setBgUserBO(BgUserBO bagUserBO) {
		this.bgUserBO = bagUserBO;
	}

	/**
	 * 根据用户login_name得到用户信息
	 * 
	 * @param singer_id
	 */
	public ModelAndView getBgUserByLoginname(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap map = new HashMap();
		ResultInfos resultInfos = new ResultInfos();
		map.put(RequestNameConstants.RESULTINFOS, resultInfos);
		String login_name = request.getParameter("login_name");
		try {
			SysUserInfo userinfo = new SysUserInfo();
			FrameLogin frameLogin = new FrameLogin();
			if (login_name != null) {
				frameLogin = bgUserBO.getFrameLogin(login_name);
				userinfo = bgUserBO.getBgUserByLoginname(login_name);
			}
			request.setAttribute("frameLogin", frameLogin);
			request.setAttribute("userinfo", userinfo);
		} catch (Exception e) {
			resultInfos.setGotoUrl("/cring/jsp/admin/bgUserList.jsp");
			resultInfos.get(0).setResultCode(
					ResultConstants.DIY_USER_MODIFY_FAILURE);
			logger.error("获取要修改用户的信息出现异常", e);
		}
		return new ModelAndView("/cring/jsp/admin/bgUserModify.jsp");
	}

	public ModelAndView updateBgUser(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap map = new HashMap();
		ResultInfos resultInfos = new ResultInfos();
		map.put(RequestNameConstants.RESULTINFOS, resultInfos);

		String update = request.getParameter("_update");
		String adapter = request.getParameter("_adapter");
		String url = request.getParameter("_url");
		AbstractDataAdapter ada = AbstractDataAdapter.getDataAdapter(request
				.getSession().getServletContext(), adapter);
		int ret = ada.update(update, request);
		if (ret < 0) {
			resultInfos.add(new ResultInfo(AdminUserRetConst.USER_UPDATE_FAIL));
		} else {
			resultInfos.add(new ResultInfo(AdminUserRetConst.USER_UPDATE_SUCCESS));
		}
		resultInfos.setGotoUrl(url);
		return new ModelAndView("/cring/jsp/admin/information.jsp",
				RequestNameConstants.INFORMATION, map);
	}

	/**
	 * 删除用户，关联清空用户角色表中对应用户信息与关联清空系统用户表信息
	 * 
	 * @param resultInfos
	 * @param singer_id
	 */
	public ModelAndView deleteBgUser(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap map = new HashMap();
		ResultInfos resultInfos = new ResultInfos();
		map.put(RequestNameConstants.RESULTINFOS, resultInfos);
		try {
			String login_name = request.getParameter("login_name");
			logger.debug("login_name： " + login_name);
			if (login_name != null && !login_name.equalsIgnoreCase("")) {
				bgUserBO.deleteBgUser(resultInfos, login_name);
				resultInfos.setGotoUrl("/cring/jsp/admin/bgUserList.jsp");
				resultInfos.get(0).setResultCode(AdminUserRetConst.USER_DELETE_SUCCESS);
			}
		} catch (Exception e) {
			resultInfos.setGotoUrl("/cring/jsp/admin/bgUserList.jsp");
			resultInfos.get(0).setResultCode(AdminUserRetConst.USER_DELETE_FAIL);
			logger.error("删除用户信息出现异常", e);
		}
		return new ModelAndView("/cring/jsp/admin/information.jsp",
				RequestNameConstants.INFORMATION, map);
	}

	public ModelAndView bindingUserAndUserGroup(HttpServletRequest request,
			HttpServletResponse response) {

		HashMap map = new HashMap();
		ResultInfos resultInfos = new ResultInfos();
		map.put(RequestNameConstants.RESULTINFOS, resultInfos);

		String insert = request.getParameter("_insert");
		String adapter = request.getParameter("_adapter");
		String url = request.getParameter("_url");
		String[] userInfoIds = request.getParameterValues("selecteds");

		int ret = -1;

		AbstractDataAdapter ada = AbstractDataAdapter.getDataAdapter(request
				.getSession().getServletContext(), adapter);

		String[] xmlString = insert.split(",");

		logger.debug("共有sql-xml配置文件" + xmlString.length + "个");

		if (null != userInfoIds) {
			for (int i = 0; i < userInfoIds.length; i++) {
				logger.info("userInfoIds====" + userInfoIds[i]);
			}
			// 对得到的xml路径字符串分组
			logger.debug("需要进行事务处理...");
			JdbcTransaction transaction = ada.getTransaction();
			try {
				int count = 0;
				ret = transaction.update(xmlString[0], request);
				logger.debug("共执行bgUserRole_delete.xml配置文件  1  次");
				for (int i = 0; i < userInfoIds.length; i++) {
					request.setAttribute("user_info_id", userInfoIds[i]);
					ret = transaction.update(xmlString[1], request);
					count++;
				}
				logger.debug("共执行bgUserRole_insert.xml配置文件 " + count + " 次");
				transaction.commit();
			} catch (Exception e) {
				logger.error("数据库操作时出错！", e);
				try {
					transaction.rollback();
				} catch (SQLException e1) {
					logger.error("数据库操作时出错！", e);
					ret = -1;
				}
				ret = -1;
			}
		} else {
			ret = ada.update(xmlString[0], request);
			logger.debug("共执行bgUserRole_delete.xml配置文件  1  次");
		}
		if (ret < 0) {
			resultInfos.add(new ResultInfo(AdminUserRetConst.USER_BINGDING_FAIL));
		} else {
			resultInfos.add(new ResultInfo(AdminUserRetConst.USER_BINGDING_SUCCESS));
		}
		resultInfos.setGotoUrl(url);
		return new ModelAndView("/cring/jsp/admin/information.jsp",
				RequestNameConstants.INFORMATION, map);
	}
}
